package com.cy.lib.manager;

import android.app.Activity;
import android.app.Application;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

/**
 * @author CY
 * @date 2014-11-24
 * @package com.src.zf.doudou
 */
public class LifeManager extends Manager {

    private Application mApp;
    private final Stack<Activity> mStack;

    protected LifeManager(Application app) {
        mApp = app;
        mStack = new Stack<>();
    }

    public static LifeManager getInstance() {
        return (LifeManager) Manager.getInstance().getLocalService(Manager.LocalService.LIFE);
    }

    public synchronized void addActivity(Activity act) {
        mStack.add(act);
    }

    public synchronized void removeActivity(Activity act) {
        mStack.remove(act);
    }

    public void removeAllActivity() {
        for (Activity act : mStack) {
            if (act != null && !act.isFinishing()) {
                act.finish();
            }
        }
        mStack.clear();
    }

    public void finishActivity(Class<?> cla) {
        Activity a = null;
        for (Activity act : mStack) {
            if (act != null && !act.isFinishing() && act.getClass().getName().equals(cla.getName())) {
                act.finish();
                a = act;
            }
        }
        if (a != null) {
            mStack.remove(a);
        }
    }

    /**
     * 结束其他它Act
     *
     * @param cla
     */
    public void finishOtherActivitys(Class<?> cla) {

        List<Activity> list = new ArrayList<>();
        Iterator<Activity> iterator = mStack.iterator();
        while (iterator.hasNext()) {
            Activity activity = iterator.next();
            if (!activity.getClass().getName().equals(cla.getName())) {
                activity.finish();
                list.add(activity);
            }
        }
        mStack.removeAll(list);
    }

    /**
     * 结束其他它Act
     *
     * @param selfAct
     */
    public void finishOtherActivitys(Activity selfAct) {
        List<Activity> list = new ArrayList<>();
        Iterator<Activity> iterator = mStack.iterator();
        while (iterator.hasNext()) {
            Activity activity = iterator.next();
            if (activity != selfAct) {
                selfAct.finish();
                list.add(activity);
            }
        }
        mStack.removeAll(list);
    }


    public Stack<Activity> getActivityStack() {
        return mStack;
    }

    public Activity getTopActivity() {
        if (!mStack.isEmpty())
            return mStack.lastElement();
        return null;
    }

    public void toMainActivity() {

        for (int i = 1; i < mStack.size(); i++) {
            mStack.get(i).finish();
        }
    }

    public boolean isEmptyStack() {
        return mStack.isEmpty();
    }

    public boolean containActivity(Class<?> cla) {
        for (Activity act : mStack) {
            if (cla.getName().equals(act.getClass().getName())) {
                return true;
            }
        }
        return false;
    }

    public Stack<Activity> getStack() {
        return mStack;
    }
}
